/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.jrcrm.service.cms;

import net.dgg.dggcloud.core.cache.CacheManager;
import net.dgg.dggcloud.core.json.JsonUtil;
import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.common.tree.Tree;
import net.dgg.dggcloud.service.jrcrm.constant.CommonConstant;
import net.dgg.dggcloud.service.jrcrm.entity.cms.TreeBook;
import net.dgg.dggcloud.service.entity.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: AudioScoreService
 * @Description:
 * @Author 汤宏
 * @date 2017/11/20 18:34
 */
@Service
public class AudioScoreService extends BaseService {
    @Autowired
    private TreeBookService treeBookService;

    /**
     * 保存录音评分
     *
     * @param array
     * @param loger
     */
    public void saveOrUpdateAudioScore(String array, User loger) {
        try {
            List<Map> list = JsonUtil.json2List(array, Map.class);
            for (int i = 0; i < list.size(); i++) {
                Map map = list.get(i);
                String name = map.get("name").toString();
                String val = map.get("ext1").toString();
                if (StringUtils.isNotEmpty(map.get("pid").toString())) {
                    Long pid = Long.parseLong(map.get("pid").toString());
                    TreeBook treeBookParent = treeBookService.queryTreeBookObj(pid);
                    if (StringUtils.isNotEmpty(map.get("id").toString())) {
                        Long id = Long.parseLong(map.get("id").toString());
                        TreeBook treeBookUp = this.repository.get(TreeBook.class, id);
                        if(null!=treeBookUp) {
                            treeBookUp.setExt1(val);
                            treeBookUp.setName(name);
                            treeBookUp.setLevels(treeBookParent.getLevels() + "_" + treeBookParent.getId());
                            treeBookUp.setUpdaterUser(loger);
                            this.repository.saveOrUpdate(treeBookUp);
                        }
                    } else {
                        TreeBook treeBook = new TreeBook();
                        Long id = keyWorker.nextId();
                        treeBook.setId(id);
                        treeBook.setName(name);
                        treeBook.setPcode(treeBookParent.getCode());
                        treeBook.setPid(treeBookParent.getId());
                        treeBook.setCode(treeBookParent.getCode() + "_" + id);
                        treeBook.setStatus(1);
                        treeBook.setLevels(treeBookParent.getLevels() + "_" + treeBookParent.getId());
                        treeBook.setSort(0);
                        treeBook.setExt1(val);
                        treeBook.setCreateUser(loger);
                        this.repository.save(treeBook);
                    }
                    CacheManager.getOperator().deleteByPattern(CommonConstant.TREEBOOK_LIST_KEY);
                }
            }

        } catch (Exception e) {
            logger.error("保存失败！" + e.getLocalizedMessage());
            throw new BizException("保存失败！" + e.getLocalizedMessage(), 10001);
        }
    }
}
